//index.js
//获取应用实例
const app = getApp()
const net = app.globalData.net

Page({
  data: {
    code: '',
    password: '',
    telphone: '',
    type: '2',
    hid: 0,
  },
  //事件处理函数
  tologPwd: function() {
    wx.navigateTo({
      url: '../logPwd/logPwd',
    })
    console.log('222')
  },
  onLoad: function(op) {
    this.setData({
      hid: op.hid,
    })
  },
  login: function(ev) {
    console.log(ev)
    console.log('手机号：', this.data.telphone, '验证码：', this.data.code, '登录方式', this.data.type)
    if (this.data.telphone && this.data.code) {
      wx.request({
        // 发起请求，提交登录信息
        url: net + '/user/login',
        data: {
          telphone: this.data.telphone,
          code: this.data.code,
          type: this.data.type
        },
        success:(res)=> { // 请求成功执行的函数
          console.log(res.data)
          if (res.data.state) {
            console.log("登录成功")
            console.log(this.data.hid);
            //登陆成功之后判断是否是处于浏览房间状态
            //如果处于浏览房间状态,返回该房间详情,否则返回首页
            if (this.data.hid) {
              //请求该房间数据信息

                  let hid = this.data.hid;
                  let uid = res.data.data.userinfo.uid;

              wx.setStorage({
                key: 'uid',
                data: uid,
                success:(res)=>{
                    console.log("strres",res);
                }
              })
                 
                  wx.reLaunch({
                    url: `/pages/home/hdetails/hdetails?hid=${hid}`
                  })


            } else {
              // 处于非浏览房间状态,返回首页
              wx.reLaunch({
                url: '../home/home'
              })
            }
          } else {
            wx.showToast({
              title: res.data.info,
              icon: 'none'
            })
          }
        },
        fail(e) {
          wx.showToast({
            title: '网络繁忙，请稍后重试',
            icon: 'none'
          })
        },
        method: 'POST'
      })
    } else if (!this.data.telphone) {
      wx.showToast({
        title: '请输入手机号码',
        icon: 'none'
      })
    } else if (!this.data.code) {
      wx.showToast({
        title: '请输入验证码',
        icon: 'none'
      })
    } else {
      wx.showToast({
        title: '未知错误',
        icon: 'none'
      })
    }
  },
  telphone: function(ev) {
    this.setData({
      telphone: ev.detail.value,
    })
  },
  code: function(ev) {
    this.setData({
      code: ev.detail.value,
    })
  },
  getNum: function() {
    wx.request({
      url: net + '/user/reg',
      data: {
        telphone: this.data.telphone,
      },
      method: 'POST',
      success: (res) => {
        console.log(res.data);
        if (res.data.info._code) { // 成功获取验证码
          wx.showToast({
            title: res.data.info._code,
            success: () => {
              console.log(res.data.info._code)
            },
          })
        } else { // 获取验证码失败
          wx.showToast({
            title: res.data.info,
            icon: 'none',
            success: () => {
              console.log(res.data.info)
            },
          })
        }
      },
      fail: (e) => {
        wx.showToast({
          title: '网络繁忙，请稍后重试',
        });
        console.log(e)
      }
    })
  }
})
